﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BugTracker.Models;
using System.Data;
using System.Data.Entity;
using BugTracker.Controllers;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
//using System.Data.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Text;
using System.IO;
using System.Data.Objects;
using System.Data.Entity.Infrastructure;
using BugTracker.Helper;

namespace BugTracker.Controllers
{
    public class ProjectManagerController : Controller
    {
        //
        // GET: /ProjectManager/
        Bug_db db = new Bug_db();
        SessionHelper sessionhelper = new SessionHelper();

        //Dashboard
        public ActionResult Home()
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
            Response.Cache.SetNoStore();

                if (sessionhelper.Session_Info() != null)
                {
                        if (sessionhelper.SessionAuth(sessionhelper.Session_Info().session_idPK) == false)
                        {
                            return RedirectToAction("Login", "Home", new { msg = "expired" });
                        }
                        else
                            {
                            string project = "projectP";
                            string issue = "issueI";

                            int project_count = (from r in db.Projects select r).Count();
                            ViewBag.Totalp = project_count;


                            int issue_count = (from i in db.Issues select i).Count();
                            ViewBag.Totali = issue_count;

                            var onlineUser = (from s in db.Sessions.Where(s => s.session_flag == 1)
                                              select s).Count();
                            ViewBag.Totalo = onlineUser;
                            return View(db.Projects.ToList()); 
                             }
                   }
                     
               
             return RedirectToAction("Login", "Home", new { msg = "expired" });

        }

        //View Project Table
        public ActionResult Project()
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
            Response.Cache.SetNoStore();

         


                if (sessionhelper.Session_Info() != null)
                {

                    if (sessionhelper.SessionAuth(sessionhelper.Session_Info().session_idPK) == false)
                    {
                        return RedirectToAction("Login", "Home", new { msg = "expired" });
                    }
                    else
                    {
                        return View(db.Projects.ToList());
                    }


                }
                return RedirectToAction("Login", "Home", new { msg = "expired" });
        
        }

        //View Issue table
        public ActionResult Issue()
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
            Response.Cache.SetNoStore();

            

                if (sessionhelper.Session_Info() != null)
                {
                    if (sessionhelper.SessionAuth(sessionhelper.Session_Info().session_idPK) == false)
                    {
                        return RedirectToAction("Login", "Home", new { msg = "expired" });
                    }
                    else
                    {
                        return View(db.Issues.ToList());
                    }
                }
                return RedirectToAction("Login", "Home", new { msg = "expired" });
          
        }

        // View Issue Details 
        public ActionResult DetailsIssue(int id)
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
            Response.Cache.SetNoStore();

            if (sessionhelper.Session_Info() != null)
            {
                if (sessionhelper.SessionAuth(sessionhelper.Session_Info().session_idPK) == false)
                {
                    return RedirectToAction("Login", "Home", new { msg = "expired" });
                }
                else
                {
                    Issue issue = db.Issues.Find(id);
                    return View(issue);
                }
            }
            return RedirectToAction("Login", "Home", new { msg = "expired" });
        }

        //Create New Project
        public ActionResult CreateProject()
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
            Response.Cache.SetNoStore();

            if (sessionhelper.Session_Info() != null)
            {
                if (sessionhelper.SessionAuth(sessionhelper.Session_Info().session_idPK) == false)
                {
                    return RedirectToAction("Login", "Home", new { msg = "expired" });
                }
                else
                {
                    ViewData["Tasks"] = (db.Tasks.OrderBy(c => c.task_assignID)).ToList();
                    ViewData["QA"] = db.Users.Where(q => q.role_idFK == 3).ToList();
                    ViewData["Developer"] = db.Users.Where(d => d.role_idFK == 2).ToList();
                    ViewData["Status"] = (db.Status.OrderBy(r => r.status_id)).ToList();

                    return View();
                }
            }
            return RedirectToAction("Login", "Home", new { msg = "expired" });
        }

        //Save new created Project
        [HttpPost]
        public ActionResult CreateProject(Task task, Project project)
        {

            project.project_datecreated = System.DateTime.Now.ToString();
            if (ModelState.IsValid)
            {

                db.Projects.Add(project);
                db.SaveChanges();
                return RedirectToAction("Project");

            }
           

            return View(project);
        }

        // View Project Details 
        public ActionResult DetailsProject(int id)
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
            Response.Cache.SetNoStore();

            if (sessionhelper.Session_Info() != null)
            {
                if (sessionhelper.SessionAuth(sessionhelper.Session_Info().session_idPK) == false)
                {
                    return RedirectToAction("Login", "Home", new { msg = "expired" });
                }
                else
                {
                    Project project = db.Projects.Find(id);
                    return View(project);
                }
            }
            return RedirectToAction("Login", "Home", new { msg = "expired" });
        }

        //Edit Project
        public ActionResult EditProject(int id)
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
            Response.Cache.SetNoStore();

            if (sessionhelper.Session_Info() != null)
            {
                if (sessionhelper.SessionAuth(sessionhelper.Session_Info().session_idPK) == false)
                {
                    return RedirectToAction("Login", "Home", new { msg = "expired" });
                }
                else
                {
                    Project project = db.Projects.Find(id);
                    ViewData["Status"] = (db.Status.OrderBy(r => r.status_id)).ToList();
                    ViewData["Tasks"] = (db.Tasks.OrderBy(c => c.task_assignID)).ToList();
                    ViewData["QA"] = db.Users.Where(q => q.role_idFK == 3).ToList();
                    ViewData["Developer"] = db.Users.Where(d => d.role_idFK == 2).ToList();

                    return View(project);
                }
            }
            return RedirectToAction("Login", "Home", new { msg = "expired" });
        }

        //Save edited Project 
        [HttpPost]
        public ActionResult EditProject(Project project)
        {
      
            if (ModelState.IsValid)
            {

                db.Entry(project).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Project");
            }
            return View(project);

        }

        //Delete Project
        public ActionResult DeleteProject(int id)
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
            Response.Cache.SetNoStore();

            if (sessionhelper.Session_Info() != null)
            {
                if (sessionhelper.SessionAuth(sessionhelper.Session_Info().session_idPK) == false)
                {
                    return RedirectToAction("Login", "Home", new { msg = "expired" });
                }
                else
                {
                    Project project = db.Projects.Find(id);
                    return View(project);
                }
            }
            return RedirectToAction("Login", "Home", new { msg = "expired" });
        }

        public ActionResult Delete(FormCollection fcNotUsed, int id = 0)
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
            Response.Cache.SetNoStore();

            if(sessionhelper.Session_Info() != null)
            {

            if (sessionhelper.SessionAuth(sessionhelper.Session_Info().session_idPK) == false)
            {
                return RedirectToAction("Login", "Home", new { msg = "expired" });

            }

            else
            {
                User user = db.Users.Find(id);
                if (user == null)
                {
                    return HttpNotFound();
                
                }
                db.Users.Remove(user);
                db.SaveChanges();
                return RedirectToAction("DeleteProject");
            }
        
        
        }

        return RedirectToAction ("Login", "Home", new { msg = "expired"});
}

        //Delete Project
        [HttpPost, ActionName("DeleteProject")]
        public ActionResult DeleteProjectConfirmed(int id)
        {
            Project project = db.Projects.Find(id);
            db.Projects.Remove(project);
            db.SaveChanges();
            return RedirectToAction("Project");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}
